package cn.tansanqinger.ThreadPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class NewWorkStealingPoolDemo {
    public static void main(String[] args) {
        //创建 4个工作线程的 任务可窃取线程池，如果不设置并行数，默认取 CPU 总核数
        ExecutorService workStealingThreadPool = Executors.newWorkStealingPool(4);
        for (int i = 0; i <10; i++) {
            final int index = i;
            workStealingThreadPool.execute(() -> {
                try {
                    //模拟任务执行时间为 任务编号为0 1 2 的执行时间需要 3秒；其余任务200 毫秒，导致任务时间差异较大
                    if (index <= 2) {
                        Thread.sleep(1000);
                    } else {
                        Thread.sleep(200);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            });
        }

        try {
            Thread.sleep(2000);//休眠 2 秒
        } catch (InterruptedException e) {}
        System.out.println("2秒后...");
    }
}
